//
//  AppDelegate.m
//  LoveFreshBeen
//
//  Created by 辛路琦 on 16/6/11.
//  Copyright © 2016年 xinluqi. All rights reserved.
//

#import "AppDelegate.h"
#import "NewfeatureViewController.h"
#import "TabBarViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

+ (AppDelegate *)sharedAppDelegate
{
    return [UIApplication sharedApplication].delegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 0.创建window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    // 1.设置根控制器
    self.window.rootViewController = [self chooseRootViewController];
    
    // 2.显示window
    [self.window makeKeyAndVisible];
    
    // 3.注册监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeRootViewController) name:@"ChangeRootViewController" object:nil];
    
    return YES;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)changeRootViewController {
    self.window.rootViewController = [self chooseRootViewController];
}

/**
 *   选择跟控制器
 */
- (UIViewController *)chooseRootViewController {
    if ([self isNewVersion]) {
        // 4.如果不一致就显示新特性, 并且保存最新的版本号
        // 4.1.创建新特性控制器
        NewfeatureViewController *newVc = [[NewfeatureViewController alloc] init];
        newVc.collectionView.backgroundColor = [UIColor redColor];
        return newVc;
        //        self.window.rootViewController = newVc;
    }else {
        // 5.如果一致就显示主页
        TabBarViewController *tabBarVc = [[TabBarViewController alloc] init];
        return tabBarVc;
        //        self.window.rootViewController = mainVc;
    }
}

/**
 *  判断是否有新版本
 *
 *  @return 如果返回YES代表有新版本
 */
- (BOOL)isNewVersion {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // 1.加载info.plist
    NSDictionary *dict = [NSBundle mainBundle].infoDictionary;
    // 2.根据CFBundleShortVersionString这个key取出当前应用程序的version
    NSString *currentVersion =  dict[@"CFBundleShortVersionString"];
    // 3.判断当前取出的版本号和以前的版本号是否一致
    NSString *sandBoxVersion = [defaults objectForKey:@"version"];
    
    // 存储当前版本号
    [defaults setObject:currentVersion forKey:@"version"];
    // 4.返回是否有新版本
    return ![currentVersion isEqualToString:sandBoxVersion];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
